// pages/chargeRecord/chargeRecord.js
const app = getApp()
var request = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    statusBarHeight: app.globalData.statusBarHeight,
    windowHeight: 0,
    scrollViewHeight: 0,
    tabs: [{
      id: 0,
      name: '充值',
      select: true
    },
    {
      id: 1,
      name: '月租',
      select: false
    },
    ],
    tebIndex: 0,
    hasTimeNextList: false,
    timeCurrentPage: 1,
    timeArray: [],
    cardCurrentPage: 0,
    cardArray: [],
    hasCardNextList: false,
    timerList: [],
    monthList: [],
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      scrollViewHeight: wx.getSystemInfoSync().windowHeight - that.data.statusBarHeight - 44 - 27 - 22 - 10
    })
    let token = wx.getStorageSync('lmtc_token');
    if (token) {
      this.initEvent()
    } else {
      this.setData({
        verifyLoginDialog: true
      })
    }
  },
  onUnload:function(){
    console.log('返回键')
    wx.redirectTo({ url: '/pages/center/center' })
  },
  initEvent() {
    this.getTimeRecord();
    this.getCardRecord();
  },
  verifyLogin() {
    this.initEvent()
  },
  /**
   * 切换tab
   * @param {*} e 
   */
  selectTab(e) {
    let index = e.target.dataset.index;
    let that = this;
    let tabs = that.data.tabs;
    for (let i = 0; i < tabs.length; i++) {
      if (i == index) {
        tabs[i].select = true
      } else {
        tabs[i].select = false;
      }
    }
    that.setData({
      tabIndex: index,
      tabs: tabs
    })
  },
  bindchange(e) {
    let index = e.detail.current
    let tabs = this.data.tabs;
    for (let i = 0; i < tabs.length; i++) {
      if (i == index) {
        tabs[i].select = true
      } else {
        tabs[i].select = false;
      }
    }
    this.setData({
      tabIndex: index,
      tabs: tabs
    })
  },
  /**
   * 获取时长记录列表
   */
  getTimeRecord() {
    let that = this;
    let url = '/customer/card-record/getRechargeCardRecord'
    request.http(url, 'GET')
      .then((res) => {
        if (res.data.code == 200) {
          let articles = res.data.data;
          that.setData({
            timerList: articles
          })
        }
      })
      .catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 加载时长充值下一页数据
   */
  loadTimeMoreData: function () {
    var that = this
    var timeCurrentPage = that.data.timeCurrentPage; // 获取当前页码
    timeCurrentPage += 1; // 加载当前页面的下一页数据
    var tips = "加载第" + (timeCurrentPage + 1) + "页";
    // 封装自己的网络请求接口
    let url = '/api/cRechargerecord/selectRechargeRecordByManage?userId=' + userId + '&page=' + timeCurrentPage + '&pageSize=10' + '&isLine=0';
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let articles = res.data.data.list;
          if (articles == null || articles.length == 0 || articles == undefined) {
            that.setData({
              articles: []
            })
          } else {
            if (res.data.data.hasNextPage) {
              that.setData({
                hasTimeNextList: true
              })
            }
            that.setData({
              ["timeArray[" + timeCurrentPage + "]"]: articles,
              timeCurrentPage: timeCurrentPage,
            })
          }

        }
      }).catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 滚动条滚动到底部触发
   */
  scrollLower: function () {
    let that = this;
    let tabIndex = that.data.tabIndex;
    let hasTimeNextList = that.data.hasTimeNextList; //时长是否还有数据
    let hasCardNextList = that.data.hasCardNextList; //月卡是否还有数据
    if (tabIndex == 0) {
      if (hasTimeNextList) {
        that.loadTimeMoreData();
      }
    } else {
      if (hasCardNextList) {
        that.loadCardMoreData();
      }
    }
  },
  /**
   * 获取月租卡记录
   */
  getCardRecord() {
    let that = this;
    let cardCurrentPage = 1;
    let url = '/customer/card-record/getMonthCardRecord'
    request.http(url, 'GET')
      .then((res) => {
        if (res.data.code == 200) {
          let articles = res.data.data;
          that.setData({
            monthList: articles
          })
        }
      })
      .catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 加载月卡充值下一页数据
   */
  loadCardMoreData: function () {
    var that = this
    var cardCurrentPage = that.data.cardCurrentPage; // 获取当前页码
    cardCurrentPage += 1; // 加载当前页面的下一页数据
    var tips = "加载第" + (cardCurrentPage + 1) + "页";
    // 封装自己的网络请求接口
    let url = '/api/bParkingleaserecord/selectRecordByManage?userId=' + userId + '&page=' + cardCurrentPage + '&pageSize=10' + '&isLine=0';
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let articles = res.data.data.list;
          if (articles == null || articles.length == 0 || articles == undefined) {
            that.setData({
              articles: []
            })
          } else {
            if (articles.length == 10) {
              that.setData({
                hasCardNextList: true
              })
            }
            for (let i = 0; i < articles.length; i++) {
              articles[i].beginTime = articles[i].beginTime.substring(0, 10);
              articles[i].endTime = articles[i].endTime.substring(0, 10);
              articles[i].parkName = '';
              let parkList = articles[i].parkList;
              for (let j = 0; j < parkList.length; j++) {
                articles[i].parkName += parkList[j].parkName;
              }
            }
            that.setData({
              ["cardArray[" + cardCurrentPage + "]"]: articles,
              cardCurrentPage: cardCurrentPage,
            })
          }

        }
      }).catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  }
})